let inputEnum = {
  // 过滤空格
  space: function ({ el, ext }) {
    // 默认-trim。可选配置：trim-过滤左右空格，left-过滤左侧空格，right-过滤右侧空格,all-过滤所有空格
    if (ext === 'all') {
      el.value = el.value.replace(/\s+/, '')
    } else if (ext === 'left') {
      el.value = el.value.replace(/(^\s*)/g, '')
    } else if (ext === 'right') {
      el.value = el.value.replace(/(\s*$)/g, '')
    } else {
      el.value = el.value.replace(/(^\s*)|(\s*$)/g, '')
    }
  },
  // 编号，只能输入英文和数字
  code: function ({ el, ext }) {
    // let str = el.value.replace(/[^\w\@\-\./]/ig, '');
    let str = el.value.replace(/[^\w/]/ig, '')
    if (str !== el.value) {
      el.value = str
    }
  },
  // 编号，只能输入英文和数字和符号
  code2: function ({ el, ext }) {
    let str = el.value.replace(/[^\w\@\-\./]/ig, '')
    console.log(str)
    if (str !== el.value) {
      console.log(555)
      el.value = str
    }
  },
  // 默认>=0的整数，可动态配置
  int: function ({ el, ext }) {
    let extObj = Object.assign({}, {
      iLen: 20,
      fLen: 0,
      minVal: 0,
      maxVal: null,
      eqMostVal: true
    }, ext)
    numeric(el, extObj)
  },
  // 默认>=0的两位小数，可动态配置
  price: function ({ el, ext }) {
    let extObj = Object.assign({}, {
      iLen: 20,
      fLen: 2,
      minVal: 0,
      maxVal: null,
      eqMostVal: true
    }, ext)
    numeric(el, extObj)
  },
  // 正数、负数、整数、小数等
  numeric: function ({ el, ext }) {
    numeric(el, ext)
  },
  intlen: function ({ el, ext }) {
    console.log(ext.len)
    let num = el.value
    // eslint-disable-next-line no-useless-escape
    num = num.replace(/[^\d\.]/g, '')
    el.value = num
    if (num.length > ext['len']) {
      el.value = num.slice(0, ext['len'])
    }
  },
  // 可自定义配置
  xxx: function ({ el, binding, ext }) {
    console.log(ext)
    el.value = 123
  }
}

// eslint-disable-next-line import/no-anonymous-default-export
const setInputValueObj =  {
  setInputValue: function (el, binding) {
    let arg = binding.arg
    let doFun = inputEnum[arg]
    if (!doFun) {
      return
    }

    let cfg = {
      el: el,
      binding: binding,
      ext: binding.ext
    }
    let flag = false
    addListener(el, 'compositionstart', () => {
      flag = true
    })
    addListener(el, 'keyup', () => {
      if (!flag) {
        doFun(cfg)
        trigger(el, 'input')
      }
    })
    addListener(el, 'compositionend', () => {
      flag = false
      doFun(cfg)
      trigger(el, 'input')
    })
  }
}

const trigger = (el, type) => {
  el.dispatchEvent(new Event('input'))
}
const addListener = function (el, type, fn) {
  el.addEventListener(type, fn, false)
}
const numeric = function (el, extObj = {}) {
  // 以下属性可配置
  extObj = extObj || {}

  // 整数位长度，默认整数20位
  let iLen = extObj.iLen || 20
  // 小数位长度，默认小数长度为0
  let fLen = extObj.fLen || 0
  // 允许最小值，默认最小值：0
  let minVal = extObj.minVal || 0
  // 允许最大值，默认无最大值
  let maxVal = extObj.maxVal || null
  // 是否[能等于极值]，默认允许等于机制，当不允许时，会比极值大/小1
  let eqMostVal = extObj.eqMostVal || true

  let num = el.value
  let minus = num.charAt(0)

  // 先把非数字的都替换掉，除了数字和.
  // eslint-disable-next-line no-useless-escape
  num = num.replace(/[^\d\.]/g, '')
  // 过滤多余小数点
  num = num.replace(/^\./g, '') // 必须保证第一个为数字而不是.
  num = num.replace(/\.{2,}/g, '.') // 保证只有出现一个.而没有多个.
  num = num.replace('.', '$#$').replace(/\./g, '').replace('$#$', '.') // 保证.只出现一次，而不能出现两次以上

  // 过滤0开头的整数，例如02
  if (num.length >= 2) {
    let first = num.charAt(0)
    if (first === '0') {
      num = num.substring(1)
    }
  }

  if (num.length > 0) {
    let dot = num.indexOf('.') > -1 && fLen > 0
    let numSplit = num.split('.')
    let intVal = numSplit[0] || '0'
    let floatVal = numSplit[1] || ''
    // 判断整数位长度
    if (intVal.length > iLen) {
      intVal = intVal.substring(0, iLen)
    }
    // 判断小数位长度
    if (floatVal !== '' && floatVal.length > fLen) {
      floatVal = floatVal.substring(0, fLen)
    }
    num = intVal + (dot ? '.' : '') + floatVal
    // 如果第一位是负号，则允许添加
    if (minus === '-') {
      num = '-' + num
    }
    // 判断允许最小值
    if (minVal !== null && minVal !== undefined) {
      if (num < minVal) {
        num = eqMostVal ? minVal : minVal + 1
      }
    }
    // 判断允许最大值
    if (maxVal !== null && maxVal !== undefined) {
      if (num > maxVal) {
        num = eqMostVal ? maxVal : maxVal - 1
      }
    }
  } else {
    // 如果第一位是负号，则允许添加
    if (minus === '-') {
      if (minVal !== null && minVal !== undefined) {
        if (minVal < 0) {
          num = '-' + num
        }
      } else {
        num = '-' + num
      }
    }
  }
  if (num !== el.value) {
    el.value = num
  }
}
  // 输入框正则匹配输入 
 export function setInputValue(e,filter) {
    if(filter) {
      filter.arg = filter.code
      setInputValueObj.setInputValue(e.target,filter)
    }
   }

 export default setInputValueObj